package com.qdxwx.models.account;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.qdxwx.models.enums.AccountStatus;
import com.qdxwx.models.enums.AccountType;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.time.LocalDateTime;

public class UserAccount implements Serializable {

    private static final long serialVersionUID = 63879730621222270L;

    protected Long userId;// 用户id
    protected Integer balance;// 账户总余额(单位分)
    protected Integer frozenAmount;// 冻结金额(单位分)
    protected Integer vmAmount;// 充值金额(单位分)
    protected Integer backAmount;// 分销返现(单位分)
    protected String accountStatus;// 账户状态
    protected Long version;// 乐观锁版本号
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime updateTime;// 最后更新时间
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    protected LocalDateTime createTime;// 创建时间

    /**
     * 入款 根据入款金额 和 入款类型 计算
     *
     * @param amount      入款金额
     * @param accountType 入款类型
     * @param amount
     */
    public void credit(int amount, AccountType accountType) {
        this.balance = this.balance + amount;
        // cash 和vm
        if (AccountType.VM == accountType) {
            this.vmAmount = this.vmAmount + amount;
        } else if (AccountType.BACK == accountType) {
            this.backAmount = this.backAmount + amount;
        } else {
            throw new RuntimeException("账户类型错误！");
        }
    }

    /**
     * 出款
     * 根据出款金额 先扣除vm,不够时扣除back(返现金额)，再扣除cash 计算
     *
     * @param amount
     */
    public void debit(int amount) {

        // 扣款时账户如果被冻结了。不准消费
        if (!AccountStatus.ACTIVE.toString().equals(this.accountStatus)) {
            throw new IllegalStateException("account status is invalid");
        }
        //账户余额不足
        if (this.balance < amount) {
            throw new IllegalStateException("account balance is insufficient");
        }
        this.balance = this.balance - amount;
        // 设置vmAmount 、backAmount ,出款时先扣除vm,不够时扣除back(返现金额)
        if (this.vmAmount >= amount) {// vm余额大于等于扣款的金额
            this.vmAmount = this.vmAmount - amount;
        } else if ((this.vmAmount + this.backAmount) >= amount) {// vm余额back(返现金额) 大于等于扣款的金额
            this.backAmount = this.backAmount - (amount - this.vmAmount);
            this.vmAmount = 0;
        } else {
            this.vmAmount = 0;// vm设置为0
            this.backAmount = 0;// back(返现金额) 设置为0
        }
    }

    public Long getUserId() {
        return userId;
    }

    public void setUserId(Long userId) {
        this.userId = userId;
    }

    public Integer getBalance() {
        return balance;
    }

    public void setBalance(Integer balance) {
        this.balance = balance;
    }

    public Integer getFrozenAmount() {
        return frozenAmount;
    }

    public void setFrozenAmount(Integer frozenAmount) {
        this.frozenAmount = frozenAmount;
    }

    public Integer getVmAmount() {
        return vmAmount;
    }

    public void setVmAmount(Integer vmAmount) {
        this.vmAmount = vmAmount;
    }

    public Integer getBackAmount() {
        return backAmount;
    }

    public void setBackAmount(Integer backAmount) {
        this.backAmount = backAmount;
    }

    public String getAccountStatus() {
        return accountStatus;
    }

    public void setAccountStatus(String accountStatus) {
        this.accountStatus = accountStatus;
    }

    public Long getVersion() {
        return version;
    }

    public void setVersion(Long version) {
        this.version = version;
    }

    public LocalDateTime getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(LocalDateTime updateTime) {
        this.updateTime = updateTime;
    }

    public LocalDateTime getCreateTime() {
        return createTime;
    }

    public void setCreateTime(LocalDateTime createTime) {
        this.createTime = createTime;
    }

    @Override
    public String toString() {
        return "UserAccount{" +
                "userId=" + userId +
                ", balance=" + balance +
                ", frozenAmount=" + frozenAmount +
                ", vmAmount=" + vmAmount +
                ", backAmount=" + backAmount +
                ", accountStatus='" + accountStatus + '\'' +
                ", version=" + version +
                ", updateTime=" + updateTime +
                ", createTime=" + createTime +
                '}';
    }
}
